home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / regexp.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  19KB  |  975 lines

  1. /* regexp.c */
  2.  
  3. /* This file contains the code that compiles regular expressions and executes
  4.  * them.  It supports the same syntax and features as vi's regular expression
  5.  * code.  Specifically, the meta characters are:
  6.  *    ^    matches the beginning of a line
  7.  *    $    matches the end of a line
  8.  *    \<    matches the beginning of a word
  9.  *    \>    matches the end of a word
  10.  *    .    matches any single character
  11.  *    []    matches any character in a character class
  12.  *    \(    delimits the start of a subexpression
  13.  *    \)    delimits the end of a subexpression
  14.  *    *    repeats the preceding 0 or more times
  15.  * NOTE: You cannot follow a \) with a *.
  16.  *
  17.  * The physical structure of a compiled RE is as follows:
  18.  *    - First, there is a one-byte value that says how many character classes
  19.  *      are used in this regular expression
  20.  *    - Next, each character class is stored as a bitmap that is 256 bits
  21.  *      (32 bytes) long.
  22.  *    - A mixture of literal characters and compiled meta characters follows.
  23.  *      This begins with M_BEGIN(0) and ends with M_END(0).  All meta chars
  24.  *      are stored as a \n followed by a one-byte code, so they take up two
  25.  *      bytes apiece.  Literal characters take up one byte apiece.  \n can't
  26.  *      be used as a literal character.
  27.  *
  28.  * If NO_MAGIC is defined, then a different set of functions is used instead.
  29.  * That right, this file contains TWO versions of the code.
  30.  */
  31.  
  32. #include <setjmp.h>
  33. #include "config.h"
  34. #include "ctype.h"
  35. #include "vi.h"
  36. #include "regexp.h"
  37.  
  38.  
  39.  
  40. static char    *previous;    /* the previous regexp, used when null regexp is given */
  41.  
  42.  
  43. #ifndef NO_MAGIC
  44. /* THE REAL REGEXP PACKAGE IS USED UNLESS "NO_MAGIC" IS DEFINED */
  45.  
  46. /* These are used to classify or recognize meta-characters */
  47. #define META        '\0'
  48. #define BASE_META(m)    ((m) - 256)
  49. #define INT_META(c)    ((c) + 256)
  50. #define IS_META(m)    ((m) >= 256)
  51. #define IS_CLASS(m)    ((m) >= M_CLASS(0) && (m) <= M_CLASS(9))
  52. #define IS_START(m)    ((m) >= M_START(0) && (m) <= M_START(9))
  53. #define IS_END(m)    ((m) >= M_END(0) && (m) <= M_END(9))
  54. #define IS_CLOSURE(m)    ((m) >= M_SPLAT && (m) <= M_RANGE)
  55. #define ADD_META(s,m)    (*(s)++ = META, *(s)++ = BASE_META(m))
  56. #define GET_META(s)    (*(s) == META ? INT_META(*++(s)) : *s)
  57.  
  58. /* These are the internal codes used for each type of meta-character */
  59. #define M_BEGLINE    256        /* internal code for ^ */
  60. #define M_ENDLINE    257        /* internal code for $ */
  61. #define M_BEGWORD    258        /* internal code for \< */
  62. #define M_ENDWORD    259        /* internal code for \> */
  63. #define M_ANY        260        /* internal code for . */
  64. #define M_SPLAT        261        /* internal code for * */
  65. #define M_PLUS        262        /* internal code for \+ */
  66. #define M_QMARK        263        /* internal code for \? */
  67. #define M_RANGE        264        /* internal code for \{ */
  68. #define M_CLASS(n)    (265+(n))    /* internal code for [] */
  69. #define M_START(n)    (275+(n))    /* internal code for \( */
  70. #define M_END(n)    (285+(n))    /* internal code for \) */
  71.  
  72. /* These are used during compilation */
  73. static int    class_cnt;    /* used to assign class IDs */
  74. static int    start_cnt;    /* used to assign start IDs */
  75. static int    end_stk[NSUBEXP];/* used to assign end IDs */
  76. static int    end_sp;
  77. static char    *retext;    /* points to the text being compiled */
  78.  
  79. /* error-handling stuff */
  80. jmp_buf    errorhandler;
  81. #define FAIL(why)    regerror(why); longjmp(errorhandler, 1)
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /* This function builds a bitmap for a particular class */
  88. static char *makeclass(text, bmap)
  89.     REG char    *text;    /* start of the class */
  90.     REG char    *bmap;    /* the bitmap */
  91. {
  92.     REG int        i;
  93.     int        complement = 0;
  94.  
  95.  
  96.     checkmem();
  97.  
  98.     /* zero the bitmap */
  99.     for (i = 0; bmap && i < 32; i++)
  100.     {
  101.         bmap[i] = 0;
  102.     }
  103.  
  104.     /* see if we're going to complement this class */
  105.     if (*text == '^')
  106.     {
  107.         text++;
  108.         complement = 1;
  109.     }
  110.  
  111.     /* add in the characters */
  112.     while (*text && *text != ']')
  113.     {
  114.         /* is this a span of characters? */
  115.         if (text[1] == '-' && text[2])
  116.         {
  117.             /* spans can't be backwards */
  118.             if (text[0] > text[2])
  119.             {
  120.                 FAIL("Backwards span in []");
  121.             }
  122.  
  123.             /* add each character in the span to the bitmap */
  124.             for (i = UCHAR(text[0]); bmap && (unsigned)i <= UCHAR(text[2]); i++)
  125.             {
  126.                 bmap[i >> 3] |= (1 << (i & 7));
  127.             }
  128.  
  129.             /* move past this span */
  130.             text += 3;
  131.         }
  132.         else
  133.         {
  134.             /* add this single character to the span */
  135.             i = *text++;
  136.             if (bmap)
  137.             {
  138.                 bmap[UCHAR(i) >> 3] |= (1 << (UCHAR(i) & 7));
  139.             }
  140.         }
  141.     }
  142.  
  143.     /* make sure the closing ] is missing */
  144.     if (*text++ != ']')
  145.     {
  146.         FAIL("] missing");
  147.     }
  148.  
  149.     /* if we're supposed to complement this class, then do so */
  150.     if (complement && bmap)
  151.     {
  152.         for (i = 0; i < 32; i++)
  153.         {
  154.             bmap[i] = ~bmap[i];
  155.         }
  156.     }
  157.  
  158.     checkmem();
  159.  
  160.     return text;
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /* This function gets the next character or meta character from a string.
  167.  * The pointer is incremented by 1, or by 2 for \-quoted characters.  For [],
  168.  * a bitmap is generated via makeclass() (if re is given), and the
  169.  * character-class text is skipped.
  170.  */
  171. static int gettoken(sptr, re)
  172.     char    **sptr;
  173.     regexp    *re;
  174. {
  175.     int    c;
  176.  
  177.     c = **sptr;
  178.     if (!c)
  179.     {
  180.         return c;
  181.     }
  182.     ++*sptr;
  183.     if (c == '\\')
  184.     {
  185.         c = **sptr;
  186.         ++*sptr;
  187.         switch (c)
  188.         {
  189.           case '<':
  190.             return M_BEGWORD;
  191.  
  192.           case '>':
  193.             return M_ENDWORD;
  194.  
  195.           case '(':
  196.             if (start_cnt >= NSUBEXP)
  197.             {
  198.                 FAIL("Too many \\(s");
  199.             }
  200.             end_stk[end_sp++] = start_cnt;
  201.             return M_START(start_cnt++);
  202.  
  203.           case ')':
  204.             if (end_sp <= 0)
  205.             {
  206.                 FAIL("Mismatched \\)");
  207.             }
  208.             return M_END(end_stk[--end_sp]);
  209.  
  210.           case '*':
  211.             return (*o_magic ? c : M_SPLAT);
  212.  
  213.           case '.':
  214.             return (*o_magic ? c : M_ANY);
  215.  
  216.           case '+':
  217.             return M_PLUS;
  218.  
  219.           case '?':
  220.             return M_QMARK;
  221. #ifndef CRUNCH
  222.           case '{':
  223.             return M_RANGE;
  224. #endif
  225.           default:
  226.             return c;
  227.         }
  228.     }
  229.     else if (*o_magic)
  230.     {
  231.         switch (c)
  232.         {
  233.           case '^':
  234.             if (*sptr == retext + 1)
  235.             {
  236.                 return M_BEGLINE;
  237.             }
  238.             return c;
  239.  
  240.           case '$':
  241.             if (!**sptr)
  242.             {
  243.                 return M_ENDLINE;
  244.             }
  245.             return c;
  246.  
  247.           case '.':
  248.             return M_ANY;
  249.  
  250.           case '*':
  251.             return M_SPLAT;
  252.  
  253.           case '[':
  254.             /* make sure we don't have too many classes */
  255.             if (class_cnt >= 10)
  256.             {
  257.                 FAIL("Too many []s");
  258.             }
  259.  
  260.             /* process the character list for this class */
  261.             if (re)
  262.             {
  263.                 /* generate the bitmap for this class */
  264.                 *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
  265.             }
  266.             else
  267.             {
  268.                 /* skip to end of the class */
  269.                 *sptr = makeclass(*sptr, (char *)0);
  270.             }
  271.             return M_CLASS(class_cnt++);
  272.  
  273.           default:
  274.             return c;
  275.         }
  276.     }
  277.     else    /* unquoted nomagic */
  278.     {
  279.         switch (c)
  280.         {
  281.           case '^':
  282.             if (*sptr == retext + 1)
  283.             {
  284.                 return M_BEGLINE;
  285.             }
  286.             return c;
  287.  
  288.           case '$':
  289.             if (!**sptr)
  290.             {
  291.                 return M_ENDLINE;
  292.             }
  293.             return c;
  294.  
  295.           default:
  296.             return c;
  297.         }
  298.     }
  299.     /*NOTREACHED*/
  300. }
  301.  
  302.  
  303.  
  304.  
  305. /* This function calculates the number of bytes that will be needed for a
  306.  * compiled RE.  Its argument is the uncompiled version.  It is not clever
  307.  * about catching syntax errors; that is done in a later pass.
  308.  */
  309. static unsigned calcsize(text)
  310.     char        *text;
  311. {
  312.     unsigned    size;
  313.     int        token;
  314.  
  315.     retext = text;
  316.     class_cnt = 0;
  317.     start_cnt = 1;
  318.     end_sp = 0;
  319.     size = 5;
  320.     while ((token = gettoken(&text, (regexp *)0)) != 0)
  321.     {
  322.         if (IS_CLASS(token))
  323.         {
  324.             size += 34;
  325.         }
  326. #ifndef CRUNCH
  327.         else if (token == M_RANGE)
  328.         {
  329.             size += 4;
  330.             while ((token = gettoken(&text, (regexp *)0)) != 0
  331.                 && token != '}')
  332.             {
  333.             }
  334.             if (!token)
  335.             {
  336.                 return size;
  337.             }
  338.         }
  339. #endif
  340.         else if (IS_META(token))
  341.         {
  342.             size += 2;
  343.         }
  344.         else
  345.         {
  346.             size++;
  347.         }
  348.     }
  349.  
  350.     return size;
  351. }
  352.  
  353.  
  354.  
  355. /* This function compiles a regexp. */
  356. regexp *regcomp(exp)
  357.     char        *exp;
  358. {
  359.     int        needfirst;
  360.     unsigned    size;
  361.     int        token;
  362.     int        peek;
  363.     char        *build;
  364. #if __STDC__
  365. # ifndef linux
  366.     volatile
  367. # endif
  368. #endif
  369.     regexp        *re;
  370. #ifndef CRUNCH
  371.     int        from;
  372.     int        to;
  373.     int        digit;
  374. #endif
  375. #ifdef DEBUG
  376.     int        calced;
  377. #endif
  378.  
  379.  
  380.     checkmem();
  381.  
  382.     /* prepare for error handling */
  383.     re = (regexp *)0;
  384.     if (setjmp(errorhandler))
  385.     {
  386.         checkmem();
  387.         if (re)
  388.         {
  389.             _free_(re);
  390.         }
  391.         return (regexp *)0;
  392.     }
  393.  
  394.     /* if an empty regexp string was given, use the previous one */
  395.     if (*exp == 0)
  396.     {
  397.         if (!previous)
  398.         {
  399.             FAIL("No previous RE");
  400.         }
  401.         exp = previous;
  402.     }
  403.     else /* non-empty regexp given, so remember it */
  404.     {
  405.         if (previous)
  406.             _free_(previous);
  407.         previous = (char *)malloc((unsigned)(strlen(exp) + 1));
  408.         if (previous)
  409.             strcpy(previous, exp);
  410.     }
  411.  
  412.     /* allocate memory */
  413.     checkmem